package com.zsoa.common.entity;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.google.common.collect.Lists;
import com.zsoa.common.base.EntityBase;

@Entity
@Table(name="app_role")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends EntityBase {
	/**
	 * 角色名称
	 */
	@Column(nullable=true,name="name",length=25,columnDefinition="varchar(25)")
	private String name;
	
	private List<String> permissionList = Lists.newArrayList();
	
	public Role(){
		
	};
	
	public Role(Long id,String name)
	{
		this.id=id;
		this.name=name;
	}
	
	/**
	 * 角色拥有的用户
	 */
	@ManyToMany(cascade={CascadeType.REFRESH},fetch=FetchType.EAGER)
	@JoinTable(name="app_user_role",joinColumns={@JoinColumn(name="roleId",referencedColumnName="id",columnDefinition="bigint(20)")},
			inverseJoinColumns={@JoinColumn(name="userId",referencedColumnName="id",columnDefinition="bigint(20)")}
			)
	private Set<User> userSet = new HashSet<User>();
	/**
	 * 角色说明
	 */
	@Column(name="description",length=200,columnDefinition="varchar(200)",nullable=true)
	private String description;
	
	
	@ElementCollection
	@CollectionTable(name = "app_role_permission", joinColumns = { @JoinColumn(name = "role_id") })
	@Column(name = "permission")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<String> getPermissionList() {
		return permissionList;
	}
	

	public void setPermissionList(List<String> permissionList) {
		this.permissionList = permissionList;
	}

	@Transient
	public String getPermissionNames() {
		List<String> permissionNameList = Lists.newArrayList();
		for (String permission : permissionList) {
			 
			permissionNameList.add(Permission.parse(permission).displayName);
		}
		return StringUtils.join(permissionNameList, ",");
	}
	
	public Set<User> getUserSet() {
		return userSet;
	}

	public void setUserSet(Set<User> userSet) {
		this.userSet = userSet;
	}

	@Column(nullable = false, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

/*
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((ids == null) ? 0 : ids.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (ids == null) {
			if (other.ids != null)
				return false;
		} else if (!ids.equals(other.ids))
			return false;
		return true;
	}
*/
}
